package com.longrise.android.byjk.plugins.course.pay;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.event.CourseDetailEvent;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.course.mycourse.MyCourseActivity;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.android.byjk.widget.view.PayItemView;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.eventbean.PayEventBean;
import com.longrise.common.utils.DZZWTools;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;


/**
 * Created by callxkj on 2017/11/19.
 * 批量购买--确认支付
 */

public class BatchPaymentActivity extends BaseActivity2<PaymentPresenter> implements
        OnPayItemListener, PaymentContract.View, View.OnClickListener {

    public static final String COURSEID_LIST = "couridlist";
    public static final String COURSENUM = "coursenum"; //课程数量
    public static final String COURSEPRICE = "courseprice"; //实付款
    public static final String KCZJ = "kczj"; //课程总价
    public static final String ZKLJ = "zklj"; //折扣立减
    public static final String ZKL = "zkl"; //折扣率
    public static final String BUY_TYPE = "buytype"; //购买途径 1-单个购买 2-批量购买 3- 团购


    private ArrayList<String> mCourseids;
    private PayItemView mPvwxzf;
    private PayItemView mPvalizf;
    private TextView mTvuserName;
    private TextView mTvzjhm;
    private TextView mTvkcsl;
    private TextView mTvkczj;
    private TextView mTvzklj;
    private TextView mTvzkl;
    private Button mBtBuy;
    private int mBuytype; //购买途径 1-单个购买 2-批量购买 3- 团购
    private RelativeLayout mRl_zklj;
    private RelativeLayout mRl_zkl;
    private LinearLayout mLLprotocol;
    private CheckBox mCbprotocol;
    private TextView mTvprotocol;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_batchpayment;
    }

    @Override
    public void initView() {

        setToolbarTitle("支付页面");
        mBtBuy = (Button) findViewById(R.id.batchpayment_buy_bt);
        mPvwxzf = (PayItemView) findViewById(R.id.batchpayment_wx);
        mPvalizf = (PayItemView) findViewById(R.id.batchpayment_zfb);
        mTvuserName = (TextView) findViewById(R.id.batchpayment_username_tv);
        mTvzjhm = (TextView) findViewById(R.id.batchpayment_zjhm_tv);
        mTvkcsl = (TextView) findViewById(R.id.batchpayment_kcsl_tv);
        mTvkczj = (TextView) findViewById(R.id.batchpayment_kczj_tv);
        mTvzklj = (TextView) findViewById(R.id.batchpayment_zklj_tv);
        mTvzkl = (TextView) findViewById(R.id.batchpayment_zkl_tv);
        mRl_zklj = (RelativeLayout) findViewById(R.id.batchpayment_zklj_rl);
        mRl_zkl = (RelativeLayout) findViewById(R.id.batchpayment_zkl_rl);

        //注册协议
        mLLprotocol = (LinearLayout) findViewById(R.id.batchpayment_protocol_ll);
        mCbprotocol = (CheckBox) findViewById(R.id.batchpayment_checkbox);
        mTvprotocol = (TextView) findViewById(R.id.batchpayment_protocol_tv);


        //购买账户 姓名 证件号码
        mTvuserName.setText(UserInfor.getInstance().getPersonname());
        mTvzjhm.setText(UserInfor.getInstance().getUsersfzh());

        mPvwxzf.setImage(R.drawable.by_ic_pay_we);
        mPvwxzf.setName(mContext.getString(R.string.wxzf));
        mPvalizf.setImage(R.drawable.by_ic_pay_ali);
        mPvalizf.setName(mContext.getString(R.string.zfbzf));
        mPvwxzf.setChecked(true);

        getIntentData();
        initEvent();
        initData();
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    private void getIntentData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        mBuytype = bundle.getInt(BUY_TYPE);
        mCourseids = bundle.getStringArrayList(COURSEID_LIST);
        String coursePrice = bundle.getString(COURSEPRICE);
        if (1 == mBuytype) { //1-单个购买
            mRl_zklj.setVisibility(View.GONE);
            mRl_zkl.setVisibility(View.GONE);
            mTvkcsl.setText("1");  //课程数量
            mTvkczj.setText(coursePrice); //课程总价
        } else if (2 == mBuytype) { //2-批量购买
            String courseNum = bundle.getString(COURSENUM);
            String zklj = bundle.getString(ZKLJ);
            String zkl = bundle.getString(ZKL);
            String kczj = bundle.getString(KCZJ);
            mTvkcsl.setText(courseNum);  //课程数量
            mTvkczj.setText(kczj); //课程总价
            mTvzklj.setText(zklj); //折扣立减
            mTvzkl.setText(zkl); //折扣率
        }
        mBtBuy.setText("支付 " + coursePrice); //按钮显示的价格
    }

    private void initEvent() {
        mPvwxzf.setOnPayItemListener(this);
        mPvalizf.setOnPayItemListener(this);
        mBtBuy.setOnClickListener(this);
        mTvprotocol.setOnClickListener(this);
        mCbprotocol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mLLprotocol.setBackgroundColor(Color.parseColor("#FAEDD4"));
                    mTvprotocol.setTextColor(Color.parseColor("#FBA140"));
                    mBtBuy.setClickable(true);
                    mBtBuy.setBackgroundResource(R.drawable.by_bg_bt_square_selector);
                } else {
                    mLLprotocol.setBackgroundColor(Color.parseColor("#F07878"));
                    mTvprotocol.setTextColor(Color.parseColor("#FFFFFF"));
                    mBtBuy.setClickable(false);
                    mBtBuy.setBackgroundColor(Color.parseColor("#D8D8D8"));
                }
            }
        });
    }

    private void initData() {
        mPresenter.openLotsOfTrain(mCourseids);
        mCbprotocol.setChecked(true);
    }

    @Override
    public void reload() {
        super.reload();
        mPresenter.openLotsOfTrain(mCourseids);
    }

    @Override
    public void onCheck(View view, Boolean checked) {
        switch (view.getId()) {
            case R.id.batchpayment_wx:
                mPvwxzf.setChecked(true);
                mPvalizf.setChecked(false);
                break;
            case R.id.batchpayment_zfb:
                mPvalizf.setChecked(true);
                mPvwxzf.setChecked(false);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.batchpayment_buy_bt: //点击购买
                toBuy();
                break;
            case R.id.dialog_courses_evaluate_bt: //支付失败
                DialogUtil.getInstance().dismiss();
                break;
            case R.id.batchpayment_protocol_tv: //批量支付协议
                startActivity(BuyProtocolActivity.class);
                break;
        }
    }

    /**
     * 购买
     */
    private void toBuy() {
        if (mPvwxzf.isChecked()) {
            mPresenter.toWxpay(mBuytype);
        } else {
            mPresenter.toAlipay(mBuytype);
        }
    }


    @Override
    public void showWxPayErrorDialog() {
        View mDialogview = View.inflate(mContext, R.layout.dialog_courses_evaluate, null);
        TextView mTvtitle = (TextView) mDialogview.findViewById(R.id.dialog_courses_title);
        mTvtitle.setText("微信支付");
        ImageView mIv = (ImageView) mDialogview.findViewById(R.id.dialog_share_success_iv);
        mIv.setImageResource(R.drawable.icon_tips);
        TextView mTvcontent = (TextView) mDialogview.findViewById(R.id.dialog_courses_content);
        mTvcontent.setText("支付失败");
        TextView mTvbt = (TextView) mDialogview.findViewById(R.id.dialog_courses_evaluate_bt);
        mTvbt.setOnClickListener(this);
        Dialog dialog = DialogUtil.getInstance().creatAlertDialog(mContext, mDialogview, 266, 195);
        dialog.setCancelable(false);
    }

    @Override
    public void showAliPayErrorDialog() {
        View mDialogview = View.inflate(mContext, R.layout.dialog_courses_evaluate, null);
        TextView mTvtitle = (TextView) mDialogview.findViewById(R.id.dialog_courses_title);
        mTvtitle.setText("支付宝支付");
        ImageView mIv = (ImageView) mDialogview.findViewById(R.id.dialog_share_success_iv);
        mIv.setImageResource(R.drawable.icon_tips);
        TextView mTvcontent = (TextView) mDialogview.findViewById(R.id.dialog_courses_content);
        mTvcontent.setText("支付失败");
        TextView mTvbt = (TextView) mDialogview.findViewById(R.id.dialog_courses_evaluate_bt);
        mTvbt.setOnClickListener(this);
        Dialog dialog = DialogUtil.getInstance().creatAlertDialog(mContext, mDialogview, 266, 195);
        dialog.setCancelable(false);
    }

    /**
     * 显示支付成功的dialog 并通知其他有关课程页面刷新
     */
    @Override
    public void showPaySuccess() {
        //课程刷新
        CourseDetailEvent event = new CourseDetailEvent();
        event.setCourseBuyStatus(true);
        EventBus.getDefault().post(event);

        View dialogview = View.inflate(this, R.layout.by_dialog_pay_success, null);
        LinearLayout ll_confirm = (LinearLayout) dialogview.findViewById(R.id.dialog_pay_success_confirm_ll);
        Dialog dialog = DialogUtil.getInstance().creatAlertDialog(this, dialogview, 270, 144);
        dialog.setCancelable(false);
        ll_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(MyCourseActivity.class);
                finish();
                //关闭前面订单详情 等页面
                PayEventBean bean = new PayEventBean();
                bean.setClosePage(true);
                EventBus.getDefault().post(bean);
            }
        });
    }

    /**
     * 支付宝 微信 支付成功 但是支付宝延迟没有给服务端回调
     */
    @Override
    public void showPayError() {
        //课程刷新
        CourseDetailEvent event = new CourseDetailEvent();
        event.setCourseBuyStatus(true);
        EventBus.getDefault().post(event);

        View dialogview = View.inflate(this, R.layout.by_dialog_pay_error, null);
        LinearLayout ll_confirm = (LinearLayout) dialogview.findViewById(R.id.dialog_pay_error_confirm_ll);
        Dialog dialog = DialogUtil.getInstance().creatAlertDialog(this, dialogview, 270, 144);
        dialog.setCancelable(false);
        ll_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(MyCourseActivity.class);
                finish();
                //关闭前面订单详情 等页面
                PayEventBean bean = new PayEventBean();
                bean.setClosePage(true);
                EventBus.getDefault().post(bean);
            }
        });
    }


    @Override
    public void showNotBackWaitDialog() {
        DZZWTools.showNotBackWaitDialog(this);
    }

    @Override
    public void dismissNotBackDialog() {
        DZZWTools.dismissNotBackDialog();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
